var links=top.location.href.substring(7).split("/");
var basePath="http://"+links[0]+"/"+links[1]+"/";
$(document).ready(function () {
    initDiggs();
});

function initDiggs() {

    var username = $.cookie("userId");

    var digged = [];
    var cookieval = $.cookie("q_digged");
    if (cookieval) {
        digged = cookieval.split(",");
    }
    $(".approve").each(function () {
        var $this = $(this);
        var id = $this.attr("qId");
        if (digged.contain(id)) {
        	$this.attr("title","已经顶过");
        	$this.attr("qId","");
        } else {
        }
    });
    $(".approve").click(function () {
    	var $this=$(this);
        if (!username) {
            if (confirm("您的操作必须登录，是否登录？")) {
                top.location.href = basePath+"login.jsp?link="+encodeURIComponent(links[2]+"/"+links[3]);
                return false;
            }
            return false;
        }
        var qId=$this.attr("qId");
        if(!qId){
        	return false;
        }
        $.post(basePath+"DiggedQServlet",{userId:username,qId:qId,action:1},function (data,status) {
        		$this.attr("qId","");
                digged.push(parseInt(qId));
                $.cookie("q_digged", digged.join(),{path:"/"});
                var num=$this.attr("num");
                $this.text(parseInt(num)+1);
                $this.attr("title","已经顶过");
        });
        return false;
    });
}
Array.prototype.contain = function(val){
	for(var i=0;i<this.length;i++){
		if(this[i] === val){
			return true;
		}
	}
	return false;
}